<script>
	import initApp from '@/common/appInit.js';
	import openApp from '@/common/openApp.js';
	// #ifdef H5
		openApp() //创建在h5端全局悬浮引导用户下载app的功能
	// #endif
	import uniIdPageInit from '@/uni_modules/uni-id-pages/init.js';
	export default {
		globalData: {
			searchText: '',
			appVersion: {},
			config: {},
			$i18n: {},
			$t: {}
		},
		onLaunch: async function() {
			console.log('App Launch')
			// 初始化云函数环境
			const db = uniCloud.database()
			
			try {
				this.globalData.$i18n = this.$i18n
				this.globalData.$t = str => this.$t(str)
				initApp();
				await uniIdPageInit()
				
				// 检查token
				const token = uni.getStorageSync('uni_id_token')
				if (token) {
					try {
						const { result } = await uniCloud.callFunction({
							name: 'uni-id-co',
							data: {
								action: 'checkToken',
								uniIdToken: token
							}
						})
						if (result.code !== 0) {
							// token无效,清除存储
							uni.removeStorageSync('uni_id_token')
							uni.removeStorageSync('uni_id_token_expired')
						}
					} catch (e) {
						console.error('校验token失败:', e)
					}
				}
			} catch(e) {
				console.error('App初始化失败:', e)
			}
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>
